﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.ComponentModel;

namespace LightControl
{
    public partial class MyLightControl : UserControl
    {
        private bool _isOn = true;
        private Color _onColor = Color.LimeGreen;
        private Color _offColor = Color.Red;
        //private Color _offColor = Color.LightGray;


        public MyLightControl()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            this.Size = new Size(60, 60); // 默认大小
            this.BackColor = Color.Transparent; // 背景透明
        }

        #region 属性

        [Category("Appearance")]
        [Description("灯是否亮起")]
        public bool IsOn
        {
            get => _isOn;
            set
            {
                if (_isOn == value) return;
                _isOn = value;
                Invalidate(); // 触发重绘
            }
        }

        [Category("Appearance")]
        [Description("灯亮起时的颜色")]
        public Color OnColor
        {
            get => _onColor;
            set
            {
                _onColor = value;
                Invalidate();
            }
        }

        [Category("Appearance")]
        [Description("灯熄灭时的颜色")]
        public Color OffColor
        {
            get => _offColor;
            set
            {
                _offColor = value;
                Invalidate();
            }
        }

        #endregion

        #region 绘制方法

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            // 计算圆形区域
            int diameter = Math.Min(Width, Height) - 4; // 留点边距
            int x = (Width - diameter) / 2;
            int y = (Height - diameter) / 2;
            Rectangle rect = new Rectangle(x, y, diameter, diameter);

            // 设置当前颜色
            Color lightColor = _isOn ? _onColor : _offColor;

            // 绘制纯色圆形
            using (Brush brush = new SolidBrush(lightColor))
            {
                g.FillEllipse(brush, rect);
            }
        }

        #endregion
    }
}
